<template>
  <a-tree
    draggable
    :tree-data="gData"
    @drop="onDrop"
    :replaceFields="{ children: 'children', title: 'label', key: 'apltable' }"
    @select="onSelect"
  >
    <div slot="title" slot-scope="{ label }">
      {{ label }}
      <!-- <a-popover placement="topLeft">
        <template slot="content">
          <div @click="addOwnlevel()"><a-icon type="plus" /> &nbsp; 添加同级</div>
          <div @click="addSubordinate()"><a-icon type="plus" />&nbsp; 添下同级</div>
        </template>
        {{ label }}
      </a-popover> -->
    </div>
  </a-tree>
</template>
<script>
function treeDataInit (data) {
  data.map(v => {
    v.scopedSlots = { title: 'title' }
    if(v.children && v.children.length > 0) {
      treeDataInit(v.children)
    }
  })
}
export default {
  props: {
    gData: {
      type: Array,
      require: true
    }
  },
  data() {
    return {
      // gData: treeConfig || [
      //   // { label: '车间', apltable: 1 },
      //   // { label: '检验室', apltable: 2},
      // ],
    }
  },
  created() {
    // treeDataInit(this.gData || [])
    // console.log('gData', this.gData)
  },
  methods: {
    // 树选中
    onSelect(ks, bol) {
      this.$emit('f_setTreeSelect', bol.node.$vnode.data.props.dataRef)
    },
    // 添加同级
    addOwnlevel() {},
    // 添加下级
    addSubordinate() {},
    // 拖动事件
    onDrop(info) {
      const dropKey = info.node.eventKey
      const dragKey = info.dragNode.eventKey
      const dropPos = info.node.pos.split('-')
      const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1])
      const loop = (data, key, callback) => {
        data.forEach((item, index, arr) => {
          // 改这里 对应 key: 'apltable'
          if (item['apltable'] === key) {
            return callback(item, index, arr)
          }
          if (item.children) {
            return loop(item.children, key, callback)
          }
        })
      }
      const data = [...this.gData]

      // Find dragObject
      let dragObj
      loop(data, dragKey, (item, index, arr) => {
        arr.splice(index, 1)
        dragObj = item
      })
      if (!info.dropToGap) {
        // Drop on the content
        loop(data, dropKey, (item) => {
          item.children = item.children || []
          // where to insert 示例添加到尾部，可以是随意位置
          item.children.push(dragObj)
        })
      } else if (
        (info.node.children || []).length > 0 && // Has children
        info.node.expanded && // Is expanded
        dropPosition === 1 // On the bottom gap
      ) {
        loop(data, dropKey, (item) => {
          item.children = item.children || []
          // where to insert 示例添加到尾部，可以是随意位置
          item.children.unshift(dragObj)
        })
      } else {
        let ar
        let i
        loop(data, dropKey, (item, index, arr) => {
          ar = arr
          i = index
        })
        if (dropPosition === -1) {
          ar.splice(i, 0, dragObj)
        } else {
          ar.splice(i + 1, 0, dragObj)
        }
      }
      this.$emit('f_setTreeData', data)
      // this.gData = data
    },
  },
}
</script>

<style lang="less" scoped>
</style>
